﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SIOCUR.Models;

namespace SIOCUR.Controllers
{
    public class OperatingConditionsController : Controller
    {

        SIOCURRepository dbpRepository = new SIOCURRepository();

        //
        // GET: /OperatingConditions/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Registra()
        {
            return View("Registra");
        }

        [HttpPost]
        public ActionResult Registra(FormCollection formValues, CONDICIONES_OPERATIVAS co)
        {
            String estacion = Session["sesionestacion"] as string;
            String sistema = Session["sesionsistema"] as string;

            if (ModelState.IsValid)
            {
                IQueryable<CONDICIONES_OPERATIVAS> Auxiliar = dbpRepository.VerificaRegistro(estacion, sistema);

                if (Auxiliar.Count() == 0)
                {
                    dbpRepository.AddOperatingConditions(estacion, sistema, DateTime.Now, Request.Form[0], Request.Form[1], Request.Form[2], Request.Form[3]);
                    var condiciones = dbpRepository.FindAllConditions().ToList();
                    return View("Agregado");
                }

                else
                {
                    return View("ErrorGuardar");
                }
                
                
            }
            else
            {
                return View();
            }
            //return View();

        }

        public ActionResult Todos()
        {
            var condiciones = dbpRepository.FindAllConditions().ToList();
            //var condiciones = dbpRepository.getConditions();
            return View(condiciones);
        }

        public ActionResult Modifica()
        {
            String estacion = Session["sesionestacion"] as string;
            String sistema = Session["sesionsistema"] as string;

            return View(dbpRepository.getConditions(estacion, sistema));
        }

        public ActionResult Modificar(int id)
        {
            CONDICIONES_OPERATIVAS co = dbpRepository.GetCondition(id);
            return View(co);
        }

        
        [HttpPost]
        public ActionResult Modificar(int id, FormCollection formValues) {
        CONDICIONES_OPERATIVAS co = dbpRepository.GetCondition(id);
        if(TryUpdateModel(co)) 
        {
            dbpRepository.Save();
            return RedirectToAction("Modifica");
        }
        return View("Modifica");
        }

    }
}
